package imPro;

import ij.ImagePlus;
import ij.process.BinaryProcessor;
import ij.process.ByteProcessor;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import imPro.helpers.Colorspace;
import imPro.helpers.EntropyThreshold;
import imPro.helpers.OtsuThresholding;

/**
 *
 * @author Ragnar
 */
public class ImageThreshold implements Runnable
{
    private Common source;
    
    public ImageThreshold(Common src)
    {
        this.source = src;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run()
    {
        while (true)
        {
            if (this.source.getImage() != null)
            {
                ImagePlus i = new ImagePlus("",this.source.getImage());
                Colorspace cp = new Colorspace(i.getProcessor());
                Object[] pixelarray = cp.getLab();
                int w = i.getWidth();
                int h = i.getHeight();

                ImageProcessor ip = new FloatProcessor(w, h, (float[])pixelarray[1], null).convertToByte(true);
                EntropyThreshold et = new EntropyThreshold(ip);
                BinaryProcessor bp = new BinaryProcessor((ByteProcessor)ip);
                bp.invert();
                this.source.setImageProcessor(bp);
            }
            
            try
            {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {}
        }
    }
}
